﻿

namespace LeetCodeTest.Graph
{
    public class DFSort
    {
        int row, col;

        int[,] tu;
        bool[,] isTravel;

        List<int>Area = new List<int>();
        int count;
        Random r = new Random();


        public DFSort(int _row,int _col)
        {
            row = _row; col = _col;
            tu = new int[row, col];

            isTravel = new bool[row, col];
            Init(row,col);
            DfsEnter();

        }

        void Init(int _row, int _col)
        {
            for (int i = 1; i < _row; i++)
            {
                for (int j = 1; j < _col; j++)
                {
                    isTravel[i - 1, j - 1] = false;
                }
            }


            for (int i = 1; i <= _row; i++)
            {
                for (int j = 1; j <= _col; j++)
                {
                    tu[i - 1, j - 1] = r.Next(0, 2);
                    //tu[i - 1, j - 1] = 0;
                    //if (j==1) tu[i - 1, j - 1] = 1;
                    //if(i==2 && j==2) tu[i - 1, j - 1] = 1;
                    //if (i == 3 && j == 3) tu[i - 1, j - 1] = 1;


                    Console.Write(tu[i - 1, j - 1] + " ");//不换行输出
                }
                Console.WriteLine(""); //换行输出
            }


        }


        void DfsEnter()
        {
            for (int n = 0; n < row; n++)
            {
                for (int m = 0; m < col; m++)
                {
                    if (!isTravel[n, m] && tu[n, m] == 1) //已遍+在岛内
                    {
                        Dfs(tu, n, m);
                        count++;
                    }


                }
            }
            Console.WriteLine($"IslandCount:__{count}");

            foreach(int num in Area)
            {
                Console.WriteLine($"area:__{num}");
            }
        }
        void Dfs(int[,] tu, int r, int c)
        {
            if (r < 0 || c < 0) return; //左上边界
            if (r >= tu.GetLength(0) || c >= tu.GetLength(1)) return;//右下边界
            if (!isTravel[r, c]) isTravel[r, c] = true;
            else return;           
            if (tu[r, c] == 0) return;
            if (Area.Count <= count) Area.Add(0);
            Area[count]++;
            Dfs(tu, r + 1, c);
            Dfs(tu, r - 1, c);
            Dfs(tu, r, c - 1);
            Dfs(tu, r, c + 1);
        }




    }
}
